import numpy as np

class StandardScaler:

    def __init__(self):

        self.mean_ = None
        self.scale_ = None

    def fit(self, X):

        self.mean_ = np.mean(X, axis = 0)
        self.scale_ = np.std(X, axis = 0)

        return self

    def transform(self,X):

        assert self.mean_ is not None and self.scale_ is not None, '请先调用fit方法'
        assert X.shape[1] == len(self.mean_), 'X的特征数需要与fit传入的数据一致'

        
        temp = np.empty(shape=X.shape, dtype = float)
        
        for col in range(X.shape[1]):
            temp[:,col] = (X[:,col] - self.mean_[col]) / self.scale_[col]

        return temp
